import Utils.RequestIOTools;
import Utils.ResultReturner;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.*;

public class KernelMain {
    public static boolean DEBUGGER_MODE = false;
    public static Vector<String> RUNNING_ARGS = new Vector<String>();
    public static void main(String[] args) {
        System.out.println("[Status] # BOOT COMPLETED #");
        System.out.println("[Logger] ==> Welcome Using Interesting Minecraft Kernel!");
        System.out.println("[Status] --> Init System... ");
        System.out.println("[Logger] --- Get Args: "+ Arrays.toString(args));
        System.out.println("[Status] --> Init Completed ~ ( A v A )");

        if(args.length == 0) {
            System.out.println("[Status] Fatal Error: No parameter input and Mode is not Debugger Mode!");
            System.exit(255);
        }

        if(args.length == 1 && args[0].equals("debugger-mode")) {
            System.out.println("[Status] Warning: Debugger Mode!");

            System.out.println("[DebugT] Args console: ");
            Scanner argsScanner = new Scanner(System.in);
            String argsString = argsScanner.next().toString();
            String[] argsArray = argsString.split("`");
            System.out.println("[DebugT] Running with args: "+Arrays.toString(argsArray));
            for(String parameter : argsArray)
                RUNNING_ARGS.add(parameter);
            System.out.println("[DebugT] Running... ");
        } else for(String parameter : args) RUNNING_ARGS.add(parameter);

        if(RUNNING_ARGS.get(0).equals("minecraft-version-list")) {

            // ( å v å ) Download Minecraft version list from Mojang website
            System.out.println("[Status] --- Downloading Minecraft version list... ");
            String versionsListString = RequestIOTools.sendRequest("https://launchermeta.mojang.com/mc/game/version_manifest.json", "GET");
            System.out.println("[Status] --- Downloaded Completed!");

            // ( A - A ) Process JSON for Output
            if(RUNNING_ARGS.size() >= 2 && RUNNING_ARGS.get(1).equals("latest")) {
                System.out.println("[Status] Mode: latest");

                JSONObject versionList = new JSONObject(versionsListString);
                JSONArray instanceVersionList = versionList.getJSONArray("versions");
                if(RUNNING_ARGS.size() == 3 && RUNNING_ARGS.get(2).equals("snapshot")) {
                    String latestSnapshotId = versionList.getJSONObject("latest").getString("snapshot");
                    JSONObject latestSnapshot = new JSONObject();
                    for(int i = 0; i < instanceVersionList.length(); i++) {
                        if(instanceVersionList.getJSONObject(i).getString("id").equals(latestSnapshotId))
                            latestSnapshot = instanceVersionList.getJSONObject(i);
                    }
                    ResultReturner.resultWithEnded(latestSnapshot.toString());
                }

                if(RUNNING_ARGS.size() == 3 && RUNNING_ARGS.get(2).equals("release")) {
                    String latestReleaseId = versionList.getJSONObject("latest").getString("release");
                    JSONObject latestRelease = new JSONObject();
                    for(int i = 0; i < instanceVersionList.length(); i++) {
                        if(instanceVersionList.getJSONObject(i).getString("id").equals(latestReleaseId))
                            latestRelease = instanceVersionList.getJSONObject(i);
                    }
                    ResultReturner.resultWithEnded(latestRelease.toString());
                }
            }

            if(RUNNING_ARGS.size() == 1 || RUNNING_ARGS.get(1).equals("all")) {
                System.out.println("[Status] Mode: all");

                ResultReturner.resultWithEnded(versionsListString);
            }
        }
    }
}
